<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Models\DailyMenu;
use App\Repositories\Manage\DailyMenuRepository;
use App\Repositories\Manage\MenuProductMappingRepository;
use App\Repositories\Manage\ProductCategoryRepository;

class DailyMenuController extends ManageBaseController
{

    private $dmRepo = NULL;

    public function __construct(
        DailyMenuRepository $dmRepo
    )
    {
        parent::__construct();
        $this->dmRepo = $dmRepo;
    }

    public function index()
    {
        $filter = request()->all();
        if (!checkIsSuper(UID)) {
            $filter['shop_id'] = intval(getUserRelateShopId(UID));
        }
        $res = $this->dmRepo->getList($filter, $this->pageNum, $this->pageSize);
        return retSuc($res);
    }

    /**
     * 待选产品
     * @param int $id
     */
    public function prod_options($menu_id = 0)
    {
        $categoryList = (new ProductCategoryRepository())->getProductsByMenuId($menu_id);

        return retSuc([
            'categoryList' => $categoryList
        ]);
    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");

        $res = $this->dmRepo->getOne($id);
        return retSuc($res);
    }

    /**
     * 菜单添加菜品
     */
    public function add_products($menu_id = 0)
    {
        if (!$menu_id)
            return retFail("参数错误");
        $productIds = request()->post("productIds");

        return (new MenuProductMappingRepository())->addProductMapping($menu_id, $productIds);
    }

    /**
     * 菜单产品列表
     */
    public function products($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return retFail("参数错误");
        }

        $menu = $this->dmRepo->getOne($id);
        if (empty($menu))
            return retFail("菜单不存在");

        $products = $this->dmRepo->getMenuProducts(intval($id));
        return retSuc($products);
    }

    /**
     *排序
     */
    public function sort()
    {
        $menu_id = intval(request()->post("id", 0));
        if (!$menu_id) {
            return retFail("参数错误");
        }

        $productIds = request()->post("productIds");
        if (empty($productIds) || !is_array($productIds)) {
            return retFail("请选择菜品");
        }
        return $this->dmRepo->sortMenuProducts($menu_id, $productIds);

    }

    /**
     * 复制菜单
     * @param null $menu_id
     */
    public function copy($menu_id = null)
    {
        $id = intval($menu_id);

        if (empty($id)) {
            return retFail("参数错误");
        }

        return $this->dmRepo->copyDailyMneu($id);
    }

    public function delete($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return retFail("参数错误");
        }
        return $this->dmRepo->deleteDailyMenu($id);
    }

    public function update($id = null)
    {
        $data = request()->only(['id', 'menu_type', 'is_can_order', 'expire_date', 'expire_end_date', 'order_start_time', 'order_end_time', 'type', 'shop_id', "title", "date", "status", "end_time", "start_time", "deliver_time"]);

        if (intval($data['menu_type']) == 0) {
            $data['expire_start_date'] = $data['expire_date'][0] ?? null;
            $data['expire_end_date'] = $data['expire_date'][1] ?? null;

            if (empty($data['expire_start_date']))
                return retFail("请填写有效起始日期");

            if (empty($data['expire_end_date']))
                return retFail("请填写有效截止日期");
            unset($data['expire_date']);
            if (!$data['id'] && DailyMenu::query()->where(['shop_id' => $data['shop_id'], 'menu_type' => $data['menu_type'], 'expire_start_date' => $data['expire_start_date'], 'expire_end_date' => $data['expire_end_date']])->count("id")) {
                return retFail("菜单已存在");
            }

        }

        $last_id = $this->dmRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail(empty($data['id']) ? "新增失败" : "更新失败");
        }
        $res = $this->dmRepo->getOne($last_id);
        return retSuc($res);
    }

    public function del_product($id = 0)
    {
        $id = intval($id);
        if (empty($id)) {
            return retFail("参数错误");
        }

        return $this->dmRepo->delMenuProduct($id);
    }

//    根据店铺获取菜单
    public function options($shop_id = 0)
    {
        $shop_id = intval(request()->get("shop_id", 0));
        if (empty($shop_id))
            return retSuc();

        $filter['shop_id'] = $shop_id;
        $filter['between']['date'] = [
            date("Y-m-d", strtotime("-2 day")),
            date("Y-m-d", strtotime("+2 day"))
        ];

        $res = $this->dmRepo->getOptions($filter);
        return retSuc($res);
    }
}
